package com.syn.vr.control;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.res.ResourcesCompat;

import com.syn.vr.R;


public class PasswordEditText extends AppCompatEditText {

    private Context mContext;
    private Drawable mEndDrawable, mCloseDrawable, mOpenDrawable, mStartDrawable;
    private boolean isClick = true;
    private Typeface mTypeface;

    public PasswordEditText(@NonNull Context context) {
        super(context);
        init(context);
    }

    public PasswordEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PasswordEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setEndDrawable(Drawable drawable) {
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        mEndDrawable = drawable;
        setCompoundDrawablesRelative(mStartDrawable, null, mEndDrawable, null);
        initPwdDrawable(mContext.getResources());
    }

    private void init(Context context) {
        mContext = context;
        mTypeface = Typeface.createFromAsset(mContext.getAssets(), "MHeiHK-Bold.TTF");
        Resources resources = context.getResources();
        Drawable[] drawables = getCompoundDrawablesRelative();
        mStartDrawable = drawables[0];
        mEndDrawable = drawables[2];
        if (mStartDrawable != null) {
            mStartDrawable.setBounds(0, 0, mStartDrawable.getIntrinsicWidth(), mStartDrawable.getIntrinsicHeight());
        }
        if (mEndDrawable != null) {
            mEndDrawable.setBounds(0, 0, mEndDrawable.getIntrinsicWidth(), mEndDrawable.getIntrinsicHeight());
            initPwdDrawable(resources);
        }
    }

    private void initPwdDrawable(Resources resources) {
        mCloseDrawable = ResourcesCompat.getDrawable(resources, R.drawable.icon_nor_close, null);
        if (mCloseDrawable != null) {
            mCloseDrawable.setBounds(0, 0, mCloseDrawable.getIntrinsicWidth(), mCloseDrawable.getIntrinsicHeight());
        }
        mOpenDrawable = ResourcesCompat.getDrawable(resources, R.drawable.icon_sel_show, null);
        if (mOpenDrawable != null) {
            mOpenDrawable.setBounds(0, 0, mOpenDrawable.getIntrinsicWidth(), mOpenDrawable.getIntrinsicHeight());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP && mEndDrawable != null) {
            boolean touchable = event.getRawX() >= (getRight() - getPaddingEnd() - mEndDrawable.getBounds().width());
            if (touchable) {
                if (isClick) {
                    this.setInputType(128);
                    isClick = false;
                    setCompoundDrawablesRelative(mStartDrawable, null, mOpenDrawable, null);
                } else {
                    this.setInputType(129);
                    isClick = true;
                    setCompoundDrawablesRelative(mStartDrawable, null, mCloseDrawable, null);
                }
                setTypeface(mTypeface);
                Editable editable = getText();
                if (editable != null) {
                    setSelection(editable.toString().length());
                }
            }
        }
        return super.onTouchEvent(event);
    }
}
